require.config({
    baseUrl: '../libs',
    paths: {
      'jquery': 'jquery',
      'underscore': 'underscore',
      'backbone': 'backbone',
      'css': 'css',
      'text': 'text',
      'cookie':'cookie',
      'indexjs':'../index/indexjs',
      'homejs':'../home/js/homejs',
      'marketjs':'../market/js/marketjs',
      'cartjs':'../cart/js/cartjs',
      'loginjs':'../login/loginjs',
      'publicjs':'../publicjs'
    }
});
require(['backbone'],function(Backbone){
  var Router = Backbone.Router.extend({
    routes:{
      '':'home',
      'home':'home',
      'market':'market',
      'cart':'cart',
      'mine':'mine',
      'login':'login',
      'cart_goods':'cart_goods',
      'sample':'php/sample.php'
    },
    home: function(){
      require(['jquery','text!../home/home.html','css!../home/css/home.css','homejs','publicjs'],function($,html,css,homejs,publicjs){
        $('#main').html(html);
      	  $.getJSON('../data/home.json',function(dat){
          	homejs.jiexi(dat);
          	publicjs.join();
          });
     
      })
    },
	market: function(){
	  	require(['jquery','text!../market/market.html','css!../market/css/market.css','marketjs','publicjs'],function($,html,css,marketjs,publicjs){
	       $('#main').html(html);
	       $.getJSON('../data/market.json',function(dat){
			marketjs.jiexi(dat);
			publicjs.join();
		})
  	  })
    },
    cart: function(){
     require(['jquery','text!../cart/cart.html','css!../cart/css/cart.css','cartjs'],function($,html,css,cartjs){
       $('#main').html(html);
       cartjs.change();
     })
    },
    cart_goods : function(){
     require(['jquery','text!../cart/cart_goods.html','css!../cart/css/car.css','publicjs'],function($,html,css,publicjs){
       $('#main').html(html);
       publicjs.join();
     })
    },
    mine: function(){
     require(['jquery','text!../mine/mine.html','css!../mine/css/mine.css'],function($,html,css){
       $('#main').html(html);

       $('.quit').on('click',function(){
//     		var date = new Date();
//			date.setTime(date.getTime() + (-1 * 24 * 60 * 60 * 1000));
//			document.cookie = "c_user=c_user.value; expires=" + date.toGMTString();
//			document.cookie = "c_pass=c_pass.value; expires=" + date.toGMTString();
//			$('.exit').attr('href','#login');
//	        console.log(document.cookie)	
	    })
     })
    },
    login: function(){
   	 require(['jquery','text!../login/login.html','css!../login/login.css','cookie','loginjs'],function($,html,css,cookie,loginjs){
       $('#main').html(html);
   
		var ok = document.querySelector('.ok');
      	var sure = document.querySelector('.sure');
		var c_user = document.querySelector('#phone');
        var c_pass = document.querySelector('#pass');

	      sure.onclick = function(){
	       	if(c_user.value == '' || c_pass.value == ''){
	       		alert('登陆失败,手机号或验证码不能为空');
	       	}else{
	       		//设置cookie值
	       		cookie.setCookie(c_user,c_user.value);
	       		var cookie1 = document.cookie;
	       		
	       		cookie.setCookie(c_pass,c_pass.value);
	       		var cookie2 = document.cookie;
	       			       		
	       		//点击登录后，跳转到对应登录界面	
	       	
       			$('.ok').attr('href','#mine');
       		
       			if($('.car_num').text()>0){
   					$('.shoppingCar').attr('href','#cart_goods');
       			}else{
       				$('.shoppingCar').attr('href','#cart');
       			}
       		
	       		
	       		//判断是否有cookie值，用户是否登录
	       		$('.my').on('click',function(){
	       			if(cookie.getCookie(c_user) == '' || cookie.getCookie(c_pass) == ''){
	       				$('.my').attr('href','#login');
	       			} else{
	       				$('.my').attr('href','#mine');
	       			}
	       		})
	       		$('.shoppingCar').on('click',function(){
	       			if($('.car_num').text()>0){
       					$('.shoppingCar').attr('href','#cart_goods');
	       			}else{
	       				$('.shoppingCar').attr('href','#cart');
	       			}
	       		})
	       	}
	     }
       
     })
    }
  });
  var router = new Router();
  Backbone.history.start();
})
